//
//  VideoViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "VideoViewController.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "VideoModel.h"
#import "VideoCell.h"
#import "TopViewController.h"

#import <MediaPlayer/MediaPlayer.h>
#import "VideoPlayerViewController.h"

@interface VideoViewController ()

@end

@implementation VideoViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.Video = [[VideoModel alloc]init];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSString *str1 = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str1 completion:^(id result) {
        
        //大图 图片
        NSString *headerViewImageStr = [[result valueForKey:@"trailer"] valueForKey:@"imageUrl"];
        UIImageView *tmpImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 210)];
        [tmpImageView setImageWithURL:[NSURL URLWithString:headerViewImageStr]];
        [self.myTableView setTableHeaderView:tmpImageView];
        [tmpImageView release];
        
        //点击图片
        UIImageView *tmpVideoImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 140, 32, 32)];
        tmpVideoImage.image = [UIImage imageNamed:@"bofang.png"];
        [tmpImageView setUserInteractionEnabled:YES];
        [tmpImageView addSubview:tmpVideoImage];
        [tmpVideoImage release];
        self.VideoImage = tmpVideoImage;
        
        //点击按钮
        UIButton *HeaderButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 210)];
        [HeaderButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [tmpImageView addSubview:HeaderButton];
        [HeaderButton release];
        self.bofangButton = HeaderButton;
        
        //大图上得标题
        NSString *headerViewTitleLabelStr = [[result valueForKey:@"trailer"] valueForKey:@"title"];
        UILabel *headerViewTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 180, self.view.frame.size.width, 30)];
        [headerViewTitleLabel setText:headerViewTitleLabelStr];
        NSLog(@"%@", headerViewTitleLabelStr);
        [headerViewTitleLabel setBackgroundColor:[UIColor colorWithRed:128.0 / 255.0 green:0.0 / 255.0 blue:128.0 / 255.0 alpha:1.0]];
        headerViewTitleLabel.font = [UIFont systemFontOfSize:14];
        [headerViewTitleLabel setTextAlignment:NSTextAlignmentCenter];
        [headerViewTitleLabel setTextColor:[UIColor colorWithRed:255.0 / 255.0 green:215.0 / 255.0 blue:0.0 / 255.0 alpha:1.0]];
        [headerViewTitleLabel setAlpha:0.7];
        [tmpImageView addSubview:headerViewTitleLabel];
        
    }];
    
    NSString *str = @"http://api.m.mtime.cn/PageSubArea/TrailerList.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        self.myArr = [result objectForKey:@"trailers"];
        
        [self.myTableView reloadData];
    }];
    
    
    UITableView *tmpTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    tmpTableView.dataSource = self;
    tmpTableView.delegate = self;
    [tmpTableView registerClass:[VideoCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:tmpTableView];
    [tmpTableView release];
    self.myTableView = tmpTableView;
    

    UIButton * rightb =[UIButton buttonWithType:UIButtonTypeSystem];
    rightb.frame =CGRectMake(280, 20, 40, 30);
    [rightb setTitle:@"精选" forState:UIControlStateNormal];
    [rightb setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [rightb addTarget:self action:@selector(rightbAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:rightb];
    
    
    //刷新
   // [self setupRefresh];
}

//右button点击事件
-(void)rightbAction:(id)sender
{
    TopViewController * topVC =[[TopViewController alloc] init];
    topVC.title =@"Max榜单";
    UINavigationController *topNavi = [[[UINavigationController alloc]initWithRootViewController:topVC]autorelease];
    
    [topNavi.navigationBar setBarTintColor:[UIColor colorWithRed:128.0 / 255.0 green:0.0 / 255.0 blue:128.0 / 255.0 alpha:1.0]];
    //title颜色
    [topNavi.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:255.0 / 255.0 green:215.0 / 255.0 blue:0.0 / 255.0 alpha:1.0], NSForegroundColorAttributeName, nil]];
    topNavi.navigationBar.translucent = NO;
    [self.navigationController pushViewController:topVC animated:YES];
}
///**
// *  集成刷新控件
// */
//- (void)setupRefresh
//{
//    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    //#warning 自动刷新(一进入程序就下拉刷新)
//    [self.myTableView headerBeginRefreshing];
//    [self.myTableView reloadData];
//    
//    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//}
//
//#pragma mark 开始进入刷新状态
//- (void)headerRereshing
//{
//    //    // 1.添加假数据
//    //    for (int i = 0; i<5; i++) {
//    //        [self.fakeData insertObject:MJRandomData atIndex:0];
//    //    }
//    
//    // 2.2秒后刷新表格UI
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        // 刷新表格
//        [self.myTableView reloadData];
//        
//        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//        [self.myTableView headerEndRefreshing];
//    });
//}
//
//- (void)footerRereshing
//{
//    //    // 1.添加假数据
//    //    for (int i = 0; i<5; i++) {
//    //        [self.fakeData addObject:MJRandomData];
//    //    }
//    
//    // 2.2秒后刷新表格UI
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        // 刷新表格
//        [self.myTableView reloadData];
//        
//        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//        [self.myTableView footerEndRefreshing];
//    });
//}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.myArr.count > 0)
    {
        return self.myArr.count;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    VideoCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell) {
        cell = [[[VideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str] autorelease];
    }
    //kvc
    NSDictionary *dic = [self.myArr objectAtIndex:indexPath.row];
    [self.Video setValuesForKeysWithDictionary:dic];
    
    //图片
    [cell.myImageView setImageWithURL:[NSURL URLWithString:self.Video.coverImg]];
    
    //标题
    [cell.titleLabel setText:self.Video.movieName];
    
    //内容
    [cell.sumLabel setText:self.Video.summary];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    VideoPlayerViewController *moviePlayer = [[[VideoPlayerViewController alloc] init] autorelease];
    //将视频地址传过去
    NSString *str = @"http://api.m.mtime.cn/PageSubArea/TrailerList.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        self.myArr = [result objectForKey:@"trailers"];
        
        moviePlayer.movieURL = [NSURL URLWithString:[[self.myArr objectAtIndex:indexPath.row] valueForKey:@"url"]];
        
        //然后播放就OK了
        [moviePlayer readyPlayer];
        [self presentModalViewController:moviePlayer animated:NO];
    }];
    
}


-(void)buttonClicked:(UIButton *)button
{
    
    NSLog(@"shhsshhshs");
    VideoPlayerViewController *moviePlayer = [[[VideoPlayerViewController alloc] init] autorelease];
    NSString *str1 = @"http://api.m.mtime.cn/PageSubArea/GetRecommendationIndexInfo.api";
    [NetworkHandler connectioonNetGETDataWithURLString:str1 completion:^(id result) {
        moviePlayer.movieURL = [NSURL URLWithString:[[result objectForKey:@"trailer"] objectForKey:@"mp4Url"]];
        
        //然后播放就OK了
        [moviePlayer readyPlayer];
        [self presentModalViewController:moviePlayer animated:NO];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
